package com.lantian.common.utils.file;

/**
 * 媒体类型工具类
 * 
 * @author lantian
 */
public class MimeTypeUtils
{
    public static final String IMAGE_PNG = "image/png";

    public static final String IMAGE_JPG = "image/jpg";

    public static final String IMAGE_JPEG = "image/jpeg";

    public static final String IMAGE_BMP = "image/bmp";

    public static final String IMAGE_GIF = "image/gif";
    
    public static final String[] IMAGE_EXTENSION = { "bmp", "gif", "jpg", "jpeg", "png" };

    public static final String[] FLASH_EXTENSION = { "swf", "flv" };

    public static final String[] MEDIA_EXTENSION = { "swf", "flv", "mp3", "wav", "wma", "wmv", "mid", "avi", "mpg",
            "asf", "rm", "rmvb" };

    public static final String[] VIDEO_EXTENSION = { "mp4", "avi", "rmvb" };

    public static final String[] DEFAULT_ALLOWED_EXTENSION = {
            // 图片
            "bmp", "gif", "jpg", "jpeg", "png",
            // word excel powerpoint
            "doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt",
            // 压缩文件
            "rar", "zip", "gz", "bz2",
            // 视频格式
            "mp4", "avi", "rmvb",
            // pdf
            "pdf" };


    // 其他MIME类型常量
    public static final String MIME_TXT = "text/plain";
    public static final String MIME_HTML = "text/html";
    public static final String MIME_XHTML = "application/xhtml+xml";
    public static final String MIME_DOC = "application/msword";
    public static final String MIME_DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    public static final String MIME_XLS = "application/vnd.ms-excel";
    public static final String MIME_XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    public static final String MIME_PPT = "application/vnd.ms-powerpoint";
    public static final String MIME_PPTX = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
    public static final String MIME_PDF = "application/pdf";
    public static final String MIME_ZIP = "application/zip";
    public static final String MIME_RAR = "application/x-rar-compressed";
    public static final String MIME_GZ = "application/gzip";
    public static final String MIME_BZ2 = "application/x-bzip2";
    public static final String MIME_MP3 = "audio/mpeg";
    public static final String MIME_WAV = "audio/wav";
    public static final String MIME_WMA = "audio/x-ms-wma";
    public static final String MIME_AVI = "video/x-msvideo";
    public static final String MIME_MP4 = "video/mp4";
    public static final String MIME_FLV = "video/x-flv";
    public static final String MIME_RMVB = "video/x-pn-realvideo";
    public static final String MIME_SWF = "application/x-shockwave-flash";
    public static final String MIME_OCTET_STREAM = "application/octet-stream";


    public static String getExtension(String prefix)
    {
        switch (prefix)
        {
            case IMAGE_PNG:
                return "png";
            case IMAGE_JPG:
                return "jpg";
            case IMAGE_JPEG:
                return "jpeg";
            case IMAGE_BMP:
                return "bmp";
            case IMAGE_GIF:
                return "gif";
            default:
                return "";
        }
    }


    /**
     * 根据文件扩展名获取MIME类型
     *
     * @param fileName 文件名或文件扩展名
     * @return MIME类型
     */
    public static String getMimeType(String fileName)
    {
        if (fileName == null || fileName.isEmpty())
        {
            return MIME_OCTET_STREAM;
        }

        // 获取文件扩展名（转为小写以便比较）
        String extension;
        int lastDotIndex = fileName.lastIndexOf('.');
        if (lastDotIndex > 0 && lastDotIndex < fileName.length() - 1)
        {
            extension = fileName.substring(lastDotIndex + 1).toLowerCase();
        }
        else
        {
            // 如果没有点号，直接使用整个文件名作为扩展名
            extension = fileName.toLowerCase();
        }

        // 根据扩展名返回对应的MIME类型
        switch (extension)
        {
            // 图片类型
            case "bmp":
                return IMAGE_BMP;
            case "gif":
                return IMAGE_GIF;
            case "jpg":
                return IMAGE_JPG;
            case "jpeg":
                return IMAGE_JPEG;
            case "png":
                return IMAGE_PNG;
            // 文本类型
            case "txt":
                return MIME_TXT;
            case "html":
            case "htm":
                return MIME_HTML;
            // Office文档类型
            case "doc":
                return MIME_DOC;
            case "docx":
                return MIME_DOCX;
            case "xls":
                return MIME_XLS;
            case "xlsx":
                return MIME_XLSX;
            case "ppt":
                return MIME_PPT;
            case "pptx":
                return MIME_PPTX;
            // PDF类型
            case "pdf":
                return MIME_PDF;
            // 压缩文件类型
            case "zip":
                return MIME_ZIP;
            case "rar":
                return MIME_RAR;
            case "gz":
                return MIME_GZ;
            case "bz2":
                return MIME_BZ2;
            // 音频类型
            case "mp3":
                return MIME_MP3;
            case "wav":
                return MIME_WAV;
            case "wma":
                return MIME_WMA;
            // 视频类型
            case "avi":
                return MIME_AVI;
            case "mp4":
                return MIME_MP4;
            case "flv":
                return MIME_FLV;
            case "rmvb":
                return MIME_RMVB;
            // Flash类型
            case "swf":
                return MIME_SWF;
            // 默认类型
            default:
                return MIME_OCTET_STREAM;
        }
    }
}
